//------------------------------------------------------------------------------
// <copyright company="Telligent Systems">
//     Copyright (c) Telligent Systems Corporation.  All rights reserved.
// </copyright> 
//------------------------------------------------------------------------------

using System;
using System.ComponentModel;
using CommunityServer.Components;
using CommunityServer.Galleries.Components;

namespace CommunityServer.Galleries.Controls
{
	/// <summary>
	/// Summary description for SidePortal.
	/// </summary>
	public class RandomPicture : GalleryImage
	{

		#region Public Properties

		[DefaultValue( -1 )]
		public virtual int SectionID
		{
			get
			{
				Object state = ViewState["SectionID"];
				if(state != null)
					return (int)state;
				return -1;
			}
			set { ViewState["SectionID"] = value; }
		}

		[DefaultValue("")]
		private string applicationKey;
		public string ApplicationKey
		{
			get { return applicationKey; }
			set { applicationKey = value; }
		}

		[DefaultValue( -1 )]
		public virtual int CategoryID
		{
			get
			{
				Object state = ViewState["CategoryID"];
				if(state != null)
					return (int)state;
				return -1;
			}
			set { ViewState["CategoryID"] = value; }
		}

		[DefaultValue( true )]
		public virtual bool UseLink
		{
			get
			{
				Object state = ViewState["UseLink"];
				if(state != null)
					return (bool)state;
				return true;
			}
			set { ViewState["UseLink"] = value; }
		}

		[DefaultValue( false )]
		public virtual bool LinkToPicture
		{
			get
			{
				Object state = ViewState["LinkToPicture"];
				if(state != null)
					return (bool)state;
				return false;
			}
			set { ViewState["LinkToPicture"] = value; }
		}
		[DefaultValue( false )]
		public virtual bool LinkToCategory
		{
			get
			{
				Object state = ViewState["LinkToCategory"];
				if(state != null)
					return (bool)state;
				return false;
			}
			set { ViewState["LinkToCategory"] = value; }
		}

		#endregion

		protected override void OnInit(EventArgs e)
		{
			base.OnInit(e);
			CSContext cSContext = CSContext.Current;
			ApplicationKey = cSContext.ApplicationKey;
			CategoryID = cSContext.CategoryID;

            // Use a different prefix for the img id attribute than the default "SmallThumb" so there won't
            //  be duplicate id's if a PictureListing control containing the random image is on the same page.
            imageIDprefix = "RandomPicture";
		}


		protected override void LoadPicture()
		{
			if((SectionID == -1) && !Globals.IsNullorEmpty(ApplicationKey))
				SectionID = Galleries.GetGalleryID( ApplicationKey );
			else if(SectionID == -1)
			{
				Gallery gallery = Galleries.GetRandomGallery(true);
				if(gallery == null)
					return;
				SectionID = gallery.SectionID;
				ApplicationKey = gallery.ApplicationKey;
			}
			GalleryPost = GalleryPosts.GetRandomPicture(SectionID, CategoryID);

			if(this.UseLink && this.LinkToPicture && ApplicationKey != "" && GalleryPost != null)
				base.NavigateUrl = GalleryUrls.Instance().ViewPicture(ApplicationKey,CategoryID, GalleryPost); 
			if(this.UseLink && this.LinkToCategory && ApplicationKey != "")
				base.NavigateUrl = GalleryUrls.Instance().ViewCategory(ApplicationKey, CategoryID) ; 
		}


	}
}
